package com.neu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neu.entity.SettleCategory;
import com.neu.service.SettleCategoryService;
import com.neu.util.JsonResult;
import com.neu.util.ResultCode;


@CrossOrigin
@RestController
@RequestMapping("/his/settlecategory")
public class SettleCategoryController {
		@Autowired
		private SettleCategoryService settleCategoryService;
//		添加结算类别
		@PostMapping("/add_sc")
		public JsonResult<Boolean> addSettleCategory(@RequestBody SettleCategory settleCategory) {
			boolean save = settleCategoryService.addSettleCategory(settleCategory);
			if(save) {
				return new JsonResult<Boolean>(ResultCode.SUCCESS,save);
			}else {
				return new JsonResult<Boolean>(ResultCode.INSERT_ERROR,save);
			}			
		}
//		删除结算类别根据id
		@PostMapping("/delete_sc")
		public JsonResult<Boolean> deleteSettleCategory(int id) {
			boolean del = settleCategoryService.deleteSettleCategory(id);
			if(del) {
				return new JsonResult<Boolean>(ResultCode.SUCCESS,del);
			}else {
				return new JsonResult<Boolean>(ResultCode.DEL_ERROR,del);
			}				
		}
//		修改结算类别
		@PostMapping("/update_sc")
		public JsonResult<Boolean> updateSettleCategory(@RequestBody SettleCategory settleCategory) {
			boolean upd = settleCategoryService.updateSettleCategory(settleCategory);
			if(upd) {
				return new JsonResult<Boolean>(ResultCode.SUCCESS,upd);
			}else {
				return new JsonResult<Boolean>(ResultCode.UPDATE_ERROR,upd);
			}	
		}
//		分页查询 结算类别 ，支持根据类别名称进行搜索
		@GetMapping("/get_sc")
		public JsonResult<List<SettleCategory>> pageSettleCategory(int page,int limit,String keyword) {
			page = (page-1)*limit;
			List<SettleCategory> list = settleCategoryService.pageSettleCategory(page, limit, keyword);
			int total = settleCategoryService.pageSettleCategoryTotal(keyword);
			return new JsonResult<List<SettleCategory>>(ResultCode.SUCCESS,list,total) ;	
		}
		
		
}
